﻿//============================================================
//author:zhxitao
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Saker.Kernel.Model;

namespace Saker.Kernel.DAL
{
    #region SysEvent数据访问层
	/// <summary>
	///SysEvent数据访问层
	/// </summary>	
	public partial class SysEventDAL
	{
        #region SysEvent数据访问层 自动生成
		/// <summary>
        /// 添加SysEvent
        /// </summary>
        /// <param name="SysEvent">SysEvent</param>
        /// <returns>添加后的SysEvent</returns>
        public SysEvent Add
			(SysEvent sysEvent)
		{
				string sql ="INSERT INTO SysEvent (LoginName, UserID, OccurTime, ModuleName, ModuleTag, EventFrom, EventType, OccurIP, EventRecord)  VALUES (@LoginName, @UserID, @OccurTime, @ModuleName, @ModuleTag, @EventFrom, @EventType, @OccurIP, @EventRecord) select @@identity";
				SqlParameter[] para = new SqlParameter[]
					{
						new SqlParameter("@LoginName", ToDBValue(sysEvent.LoginName)),
						new SqlParameter("@UserID", ToDBValue(sysEvent.UserID)),
						new SqlParameter("@OccurTime", ToDBValue(sysEvent.OccurTime)),
						new SqlParameter("@ModuleName", ToDBValue(sysEvent.ModuleName)),
						new SqlParameter("@ModuleTag", ToDBValue(sysEvent.ModuleTag)),
						new SqlParameter("@EventFrom", ToDBValue(sysEvent.EventFrom)),
						new SqlParameter("@EventType", ToDBValue(sysEvent.EventType)),
						new SqlParameter("@OccurIP", ToDBValue(sysEvent.OccurIP)),
						new SqlParameter("@EventRecord", ToDBValue(sysEvent.EventRecord)),
					};
					
				int newId = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, para));
				return GetByEventID(newId);
		}

		/// <summary>
        /// 根据主键删除SysEvent
        /// </summary>
        /// <param name="EventID">EventID</param>
        /// <returns>影响的行数</returns>
        public int DeleteByEventID(int eventID)
		{
            string sql = "DELETE SysEvent WHERE EventID = @EventID";

           SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@EventID", eventID)
			};
		
            return SqlHelper.ExecuteNonQuery(sql, para);
		}
		
				
		/// <summary>
		/// 更新SysEvent
		/// </summary>
		/// <param name="SysEvent">SysEvent</param>
		/// <returns></returns>
        public int Update(SysEvent sysEvent)
        {
            string sql =
                "UPDATE SysEvent " +
                "SET " +
			" LoginName = @LoginName" 
                +", UserID = @UserID" 
                +", OccurTime = @OccurTime" 
                +", ModuleName = @ModuleName" 
                +", ModuleTag = @ModuleTag" 
                +", EventFrom = @EventFrom" 
                +", EventType = @EventType" 
                +", OccurIP = @OccurIP" 
                +", EventRecord = @EventRecord" 
               
            +" WHERE EventID = @EventID";


			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@EventID", sysEvent.EventID)
					,new SqlParameter("@LoginName", ToDBValue(sysEvent.LoginName))
					,new SqlParameter("@UserID", ToDBValue(sysEvent.UserID))
					,new SqlParameter("@OccurTime", ToDBValue(sysEvent.OccurTime))
					,new SqlParameter("@ModuleName", ToDBValue(sysEvent.ModuleName))
					,new SqlParameter("@ModuleTag", ToDBValue(sysEvent.ModuleTag))
					,new SqlParameter("@EventFrom", ToDBValue(sysEvent.EventFrom))
					,new SqlParameter("@EventType", ToDBValue(sysEvent.EventType))
					,new SqlParameter("@OccurIP", ToDBValue(sysEvent.OccurIP))
					,new SqlParameter("@EventRecord", ToDBValue(sysEvent.EventRecord))
			};

			return SqlHelper.ExecuteNonQuery(sql, para);
        }		
		
		/// <summary>
		/// 根据主键查询SysEvent实体
		/// </summary>
		/// <param name="SysEvent">SysEvent</param>
		/// <returns>SysEvent</returns>
        public SysEvent GetByEventID(int eventID)
        {
            string sql = "SELECT * FROM SysEvent WHERE EventID = @EventID";
            using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, new SqlParameter("@EventID", eventID)))
			{
				if (reader.Read())
				{
					return ToModel(reader);
				}
				else
				{
					return null;
				}
       		}
        }
		/// <summary>
		/// SqlDataReader对象转换为SysEvent实体
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <returns>SysEvent实体</returns>
		public SysEvent ToModel(SqlDataReader reader)
		{
			SysEvent sysEvent = new SysEvent();

			sysEvent.EventID = (int)ToModelValue(reader,"EventID");
			sysEvent.LoginName = (string)ToModelValue(reader,"LoginName");
			sysEvent.UserID = (int?)ToModelValue(reader,"UserID");
			sysEvent.OccurTime = (DateTime)ToModelValue(reader,"OccurTime");
			sysEvent.ModuleName = (string)ToModelValue(reader,"ModuleName");
			sysEvent.ModuleTag = (string)ToModelValue(reader,"ModuleTag");
			sysEvent.EventFrom = (string)ToModelValue(reader,"EventFrom");
			sysEvent.EventType = (byte)ToModelValue(reader,"EventType");
			sysEvent.OccurIP = (string)ToModelValue(reader,"OccurIP");
			sysEvent.EventRecord = (string)ToModelValue(reader,"EventRecord");
			return sysEvent;
		}
		/// <summary>
		/// 得到数据总行数
		/// </summary>
		/// <returns></returns>
		public int GetTotalCount()
		{
			string sql = "SELECT count(1) FROM SysEvent";
			return (int)SqlHelper.ExecuteScalar(sql);
		}
		/// <summary>
		/// 分页，此方法仅在sql 2005下使用
		/// </summary>
		/// <param name="minrownum">起始行数</param>
		/// <param name="maxrownum">>结束行数</param>
		/// <returns>SysEvent集合</returns>
		public IEnumerable<SysEvent> GetPagedData(int minrownum,int maxrownum)
		{
			string sql = "SELECT * from(SELECT *,row_number() over(order by EventID) rownum FROM SysEvent) t where rownum>=@minrownum and rownum<=@maxrownum";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql,
				new SqlParameter("@minrownum",minrownum),
				new SqlParameter("@maxrownum",maxrownum)))
			{
				return ToModels(reader);					
			}
		}
        /// <summary>
        /// 分页，调用存储过程
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="recordCount">记录数</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="whereCondition">where</param>
        /// <returns>DataTable数据集</returns>
        public DataTable GetPagingRecord(int pageIndex, ref int recordCount, int pageSize, ref int pageCount,string whereCondition)
        {
            return SqlHelper.GetPagingRecord(pageIndex,ref recordCount,pageSize,ref pageCount,"SysEvent","EventID",whereCondition,"*","EventID",true);
        }
		/// <summary>
		/// 得到所有SysEvent集合
		/// </summary>
		/// <returns>所有集合</returns>
		public IEnumerable<SysEvent> GetAll()
		{
			string sql = "SELECT * FROM SysEvent";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
			{
				return ToModels(reader);			
			}
		}
		/// <summary>
		/// SqlDataReader对象转换到SysEvent集合
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <returns>SysEvent集合</returns>
		protected IEnumerable<SysEvent> ToModels(SqlDataReader reader)
		{
			var list = new List<SysEvent>();
			while(reader.Read())
			{
				list.Add(ToModel(reader));
			}	
			return list;
		}		
		/// <summary>
		/// 转换数据值
		/// </summary>
		/// <param name="value">object类型</param>
		/// <returns>object对象</returns>
		protected object ToDBValue(object value)
		{
			if(value==null)
			{
				return DBNull.Value;
			}
			else
			{
				return value;
			}
		}
		/// <summary>
		/// 转换实体值
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <param name="columnName">列名称</param>
		/// <returns>值</returns>
		protected object ToModelValue(SqlDataReader reader,string columnName)
		{
			if(reader.IsDBNull(reader.GetOrdinal(columnName)))
			{
				return null;
			}
			else
			{
				return reader[columnName];
			}
		}
        #endregion
	}
    #endregion
}
